package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPageQueryVO;
import com.sky.vo.OrderStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@Slf4j
@Api(tags = "订单相关接口")
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 分页搜索订单
     * @param ordersPageQueryDTO
     * @return
     */
    @ApiOperation(value = "分页搜索订单")
    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("ordersPageQueryDTO:{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.getHistoryOrders(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @ApiOperation(value = "各个状态的订单数量统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> getOrderStatistics()
    {
        OrderStatisticsVO orderStatisticsVO = orderService.getOrderStatistics();
        return Result.success(orderStatisticsVO);
    }

    @ApiOperation(value = "查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderPageQueryVO> getOrderDetail(@PathVariable Long id)
    {
        log.info("id:{}", id);
        OrderPageQueryVO orderPageQueryVO = orderService.getOrderDetailById(id);
        return Result.success(orderPageQueryVO);
    }

    /**
     * 接订单
     * @param ordersConfirmDTO
     * @return
     */
    @ApiOperation(value = "接单")
    @PutMapping("/confirm")
    public Result acceptOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO)
    {
        log.info("ordersConfirmDTO:{}", ordersConfirmDTO);
        ordersConfirmDTO.setStatus(Orders.CONFIRMED);
        orderService.acceptOrder(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     * @param ordersRejectionDTO
     * @return
     */
    @ApiOperation(value = "拒单")
    @PutMapping("/rejection")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO)
    {
        log.info("ordersRejectionDTO:{}", ordersRejectionDTO);
        orderService.rejectOrders(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PutMapping("/cancel")
    public Result cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO)
    {
        log.info("ordersCancelDTO:{}", ordersCancelDTO);
        orderService.cancelOrder(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @ApiOperation(value = "派送订单")
    @PutMapping("/delivery/{id}")
    public Result deliverOrder(@PathVariable Long id)
    {
        log.info("id:{}", id);
        OrdersConfirmDTO ordersConfirmDTO = OrdersConfirmDTO.builder()
                .id(id)
                .status(Orders.DELIVERY_IN_PROGRESS)
                .build();
        orderService.acceptOrder(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @ApiOperation(value = "完成订单")
    @PutMapping("/complete/{id}")
    public Result finishOrder(@PathVariable Long id)
    {
        log.info("id:{}", id);
        orderService.finishOrder(id);
        return Result.success();
    }

}
